package com.atguigu.tingshu.order.api;

import com.atguigu.tingshu.common.login.annotation.TingshuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order/orderInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class OrderInfoApiController {

    @Autowired
    private OrderInfoService orderInfoService;


    // Request URL: http://192.168.200.1:8500/api/order/orderInfo/trade

    @PostMapping("/trade")
    @TingshuLogin
    @Operation(summary = "结算页的展示,看都买了那些商品")
    public Result trade(@RequestBody TradeVo tradeVo) {

        OrderInfoVo orderInfoVo = orderInfoService.trade(tradeVo);

        List<Long> exitItemIdList = orderInfoVo.getExitItemIdList();
        if (!CollectionUtils.isEmpty(exitItemIdList)) {
            return Result.fail(orderInfoVo);
        }

        return Result.ok(orderInfoVo);
    }


    // Request URL: http://192.168.200.1:8500/api/order/orderInfo/submitOrder

    /**
     * @Validated 使用的是spring的校验注解，有了它，OrderInfoVo类中属性上面的 @NotEmpty()才生效
     * @param orderInfoVo
     * @return
     */
    @PostMapping("/submitOrder")
    @TingshuLogin
    @Operation(summary = "提交订单")
    public Result submitOrder(@RequestBody @Validated OrderInfoVo orderInfoVo) {//前端提交和返回的都是OrderInfoVo
//        orderNoMap订单编号，里面只放了一个订单编号
        Map<String, Object> orderNoMap = orderInfoService.submitOrder(orderInfoVo);

        return Result.ok(orderNoMap);
    }

    // Request URL: http://localhost:8500/api/order/orderInfo/getOrderInfo/GVK9UeYEiSHG
    @GetMapping("/getOrderInfo/{orderNo}")
    @TingshuLogin
    @Operation(summary = "根据订单编号查询订单")
    public Result<OrderInfo> getOrderInfo(@PathVariable(value = "orderNo") String orderNo) {
        OrderInfo orderInfo = orderInfoService.getOrderInfo(orderNo, AuthContextHolder.getUserId());

        return Result.ok(orderInfo);
    }

    // Request URL: http://192.168.200.1:8500/api/order/orderInfo/findUserPage/1/10

    @GetMapping("/findUserPage/{pn}/{pz}")
    @TingshuLogin
    @Operation(summary = "分页查询我的订单")
    public Result findUserPage(@PathVariable(value = "pn") Long pn,
                               @PathVariable(value = "pz") Long pz) {


        IPage<OrderInfo> orderInfoPage = new Page<>(pn, pz);

        orderInfoPage = orderInfoService.findUserPage(orderInfoPage, AuthContextHolder.getUserId());

        return Result.ok(orderInfoPage);

    }

}

